package com.bieyang.anjia.net;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.bieyang.anjia.module.FileModel;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;

public class FileLoadManager {
	/**
	 * 三连拍第一张图片的七牛key1
	 */
	public static String key1 = "";
	/**
	 * 三连拍第一张图片的七牛key2
	 */
	public static String key2 = "";
	/**
	 * 三连拍第一张图片的七牛key3
	 */
	public static String key3 = "";
	/**
	 * 文件大小
	 */
	public static int fileSize;

	/**
	 * 通过文件path上传
	 * 
	 * @param fileModel
	 *            文件模型
	 * @param callback
	 */
	public static void uploadByPath(final FileModel fileModel,
			final UploadCallback callback) {

		UploadManager uploadManager = new UploadManager();
		uploadManager.put(fileModel.filePath, fileModel.key, fileModel.token,
				new UpCompletionHandler() {

					@Override
					public void complete(String arg0, ResponseInfo arg1,
							JSONObject arg2) {
						getQiniuKeyBack(fileModel, arg0, arg1, arg2, callback);
					}
				}, new UploadOptions(null, null, false,
						new UpProgressHandler() {

							@Override
							public void progress(String key, double percent) {
								System.out.println("key=" + key + ": percent="
										+ (int) (percent * 100) + "%");
							}
						}, null));
	}

	/**
	 * 通过文件file上传
	 * 
	 * @param fileModel
	 *            文件模型
	 * @param callback
	 */
	public static void uploadByFile(final FileModel fileModel,
			final UploadCallback callback) {

		UploadManager uploadManager = new UploadManager();
		uploadManager.put(fileModel.file, fileModel.key, fileModel.token,
				new UpCompletionHandler() {

					@Override
					public void complete(String arg0, ResponseInfo arg1,
							JSONObject arg2) {
						getQiniuKeyBack(fileModel, arg0, arg1, arg2, callback);
					}
				}, new UploadOptions(null, null, false,
						new UpProgressHandler() {

							@Override
							public void progress(String key, double percent) {
								System.out.println("key=" + key + ": percent="
										+ (int) (percent * 100) + "%");
							}
						}, null));
	}

	/**
	 * 通过文件data[]上传
	 * 
	 * @param fileModel
	 *            文件模型
	 * @param callback
	 */
	public static void uploadByData(final FileModel fileModel,
			final UploadCallback callback) {
		UploadManager uploadManager = new UploadManager();
		uploadManager.put(fileModel.data, fileModel.key, fileModel.token,
				new UpCompletionHandler() {
					@Override
					public void complete(String arg0, ResponseInfo arg1,
							JSONObject arg2) {
						getQiniuKeyBack(fileModel, arg0, arg1, arg2, callback);
					}
				}, new UploadOptions(null, null, false,
						new UpProgressHandler() {
							@Override
							public void progress(String key, double percent) {
								System.out.println("key=" + key + ": percent="
										+ (int) (percent * 100) + "%");
							}
						}, null));
	}

	/**
	 * 上传完成回调
	 * 
	 * @param fileModel
	 *            文件模型
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param callback
	 */
	private static void getQiniuKeyBack(FileModel fileModel, String arg0,
			ResponseInfo arg1, JSONObject arg2, UploadCallback callback) {

		if (arg0 != null) {
			System.out.println(arg0);
		}
		if (null != arg1) {
			System.out.println(arg1.toString());
		}
		System.out.println("ok=" + arg1.isOK());
		if (null != arg2) {
			System.out.println(arg2.toString());
			try {
				String key = arg2.getString("key");
				fileSize += arg2.getInt("fsize");
				System.out.println("key=" + key);
				if (fileModel.filePath.contains("take3")) {

					if (fileModel.alarmType.contains("take31")) {
						key1 = key;
					} else if (fileModel.alarmType.contains("take32")) {
						key2 = key;
					} else if (fileModel.alarmType.contains("take33")) {
						key3 = key;
					}
					if (!key1.isEmpty() && !key2.isEmpty() && !key3.isEmpty()) {
						String take3Key = key1 + ";" + key2 + ";" + key3;
						key1 = "";
						key2 = "";
						key3 = "";
						callback.callback(fileSize, take3Key, "01");
						fileSize = 0;
						System.out.println("take3Key = " + take3Key);
					}
				} else if (fileModel.filePath.contains("take1")) {
					callback.callback(fileSize, key, "01");
					fileSize = 0;
				} else {
					callback.callback(fileSize, key, "02");
					fileSize = 0;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public interface UploadCallback {
		public void callback(int fileSize, String key, String fileType);
	}
}
